package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 地址管理服务
 */
@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {

    //注入dao
    @Autowired
    private AddressDao addressDao;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Address> page = addressDao.findPageByCondition(queryString);
        long total = page.getTotal();   //查询总数
        List<Address> addressList = page.getResult();   //查询集合
        return new PageResult(total,addressList);
    }

    /**
     * 新增地址
     * @param address
     * @return
     */
    @Override
    public void addPlace(Address address) {
        addressDao.addPlace(address);
    }

    /**
     * 查询所有地址信息
     * @return
     */
    @Override
    public List<Address> findAll() {
        return addressDao.findAll();
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @Override
    public void delete(int id) {
        addressDao.deleteById(id);
    }

    /**
     * 查询所有地址名称
     * @return
     */
    public List<String> findLocation() {
        List<Address> addressList = addressDao.findAll();
        List<String> locationList = new ArrayList<>();
        for (Address address : addressList) {
            locationList.add(address.getLocation());
        }
        return locationList;
    }
}
